package com.qinyu.treehole.controller;

import com.qinyu.treehole.entities.R;
import com.qinyu.treehole.service.FeedService;
import com.qinyu.treehole.service.UserService;
import com.qinyu.treehole.tool.SearchTool;
import com.qinyu.treehole.vo.FeedVo;
import com.qinyu.treehole.vo.LoginFeedVo;
import com.qinyu.treehole.vo.RelationLoginUserVo;
import com.qinyu.treehole.vo.RelationUserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/search")
@Slf4j
@Validated
public class SearchController {

    @Autowired
    private UserService userService;
    @Autowired
    private FeedService feedService;

    @GetMapping("/user")
    public R searchUser(@RequestParam("keyword")String name){
        List<RelationUserVo> users = userService.searchAllUserByName(name);
        return R.ok(users, SearchTool.SEARCH_USER_SUCCESS);
    }

    @GetMapping("/user/u")
    public R searchUserLogin(@RequestParam("userId")Integer userId,
                             @RequestParam("keyword")String name){
        log.info("用户id:{}查询用户名为:{}中...", userId, name);
        List<RelationLoginUserVo> users = userService.searchAllUserLoginByName(name, userId);
        return R.ok(users, SearchTool.SEARCH_USER_SUCCESS);
    }

    @GetMapping("/feed")
    public R searchFeed(@RequestParam("keyword")String content){
        List<FeedVo> feeds = feedService.searchAllFeedByContent(content);
        return R.ok(feeds, SearchTool.SEARCH_FEED_SUCCESS);
    }

    @GetMapping("/feed/u")
    public R searchFeedLogin(@RequestParam("userId")Integer userId,
                             @RequestParam("keyword")String content){
        log.info("用户id:{}查询内容为:{}的动态中...", userId, content);
        List<LoginFeedVo> feeds = feedService.searchAllFeedLoginByContent(content, userId);
        return R.ok(feeds, SearchTool.SEARCH_FEED_SUCCESS);
    }
}
